﻿package k.util;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Iterator;
import java.util.ArrayList;

import k.db.DBConfig;
import k.server.LogConfig;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class XmlUtil {
	
	

	public static DBConfig ReadDBConfig(String fileName) {
		String elemValue;
		File inputXml = new File(fileName);
		SAXReader saxReader = new SAXReader();
		DBConfig config = new DBConfig();
		try {
			Document document = saxReader.read(inputXml);
			Element elems = document.getRootElement();

			elemValue = elems.attributeValue("dburl");
			config.setUrl(elemValue);
			elemValue = elems.attributeValue("user");
			config.setUsername(elemValue);
			elemValue = elems.attributeValue("passwd");
			config.setPassword(elemValue);
			return config;
		} catch (DocumentException e) {
			System.out.println(e.getMessage());
		}
		
		return null;
	}
	
	
	
	

	@SuppressWarnings("unchecked")
	public static List<LogConfig> ReadLogConfig(String fileName) {
		
		List<LogConfig> configs = new ArrayList<LogConfig>();
		File inputXml = new File(fileName);
		SAXReader saxReader = new SAXReader();
		try {
			Document document = saxReader.read(inputXml);
			Element elems = document.getRootElement();
			
			for (Iterator<Element> i = elems.elementIterator(); i.hasNext();) {
				Element nodes = i.next();
				LogConfig config = new LogConfig();
				
				config.setFilenamepre(nodes.element("filenamepre").getText());
				config.setLogpath(nodes.element("logpath").getText());
				config.setPattern(nodes.element("pattern").getText());
				config.setSql(nodes.element("sql").getText());
				config.setCreatetable(nodes.element("createtable").getText());
				List<String> typeList = new ArrayList<String>()  ;
				String[] types = nodes.element("fieldtype").getText().split(",");
				for (int j = 0; j < types.length; j++) {
					typeList.add(types[j]);
				}
				config.setTypes(typeList);
				configs.add(config);
			}
		} catch (DocumentException e) {
			System.out.println(e.getMessage());
		}
		return configs;
	}

	@SuppressWarnings("unchecked")
	public static void initSysFile(String fileName,
			Map<String, String> mapconf, ArrayList<String> list) {
		String elemValue;
		File inputXml = new File(fileName);
		SAXReader saxReader = new SAXReader();
		try {
			Document document = saxReader.read(inputXml);
			Element elems = document.getRootElement();
			elemValue = elems.attributeValue("billingversion");
			mapconf.put("billingversion", elemValue);
			elemValue = elems.attributeValue("logloadtime");
			mapconf.put("logloadtime", elemValue);
			// elemValue = elems.attributeValue("logpath");
			// mapconf.put("logPath", elemValue);
			elemValue = elems.attributeValue("mysqlurl");
			mapconf.put("mysqlurl", elemValue);
			elemValue = elems.attributeValue("user");
			mapconf.put("user", elemValue);
			elemValue = elems.attributeValue("passwd");
			mapconf.put("passwd", elemValue);
			for (Iterator<Element> i = elems.elementIterator(); i.hasNext();) {
				Element elem = i.next();

				if (elem.getName().equals(
						mapconf.get("billingversion").toString())) {
					for (Iterator<Element> j = elem.elementIterator(); j.hasNext();) {
						Element nodes = j.next();
						list.add(nodes.getName());
						for (Iterator<Element> k = nodes.elementIterator(); k.hasNext();) {
							Element node = k.next();
							// System.out.println(node.getName() + ":" +
							// node.getText());
							mapconf.put(nodes.getName() + node.getName(),
									node.getText());
						}
					}
				}
			}
		} catch (DocumentException e) {
			System.out.println(e.getMessage());
		}
	}
}
